import { _decorator, Component, director, ImageAsset, instantiate, MeshRenderer, Node, Prefab, SpriteFrame, Texture2D } from 'cc';
import ResManager from './ResManager';
import WorldManager from './WorldManager';
import { Plane } from './Plane';
const { ccclass, property } = _decorator;

@ccclass('AddPanel')
export class AddPanel extends Component {
    start() {

    }

    update(deltaTime: number) {
        
    }

    public async add1(parent:Node){
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>('prefab/build-1')
        let texture = await ResManager.instance.loadAssetByUrl<ImageAsset>('img/build-1')

        this.addBuild(prefab, texture)
    }

    public async add2(parent:Node){
        let prefab = await ResManager.instance.loadAssetByUrl<Prefab>('prefab/build-1')
        let texture = await ResManager.instance.loadAssetByUrl<ImageAsset>('img/build-2')
        this.addBuild(prefab, texture)
    }

    async addBuild(prefab:Prefab, texture:ImageAsset){
        let a = new Texture2D()
        a.image = texture

        let node = instantiate(prefab);
        node.getComponent(MeshRenderer).materials[0].setProperty('mainTexture', a)

        node.parent = WorldManager.instance.pickerNode
        WorldManager.instance.pickerNode.getComponent(Plane).revertColor()
        WorldManager.instance.pickerNode = null

        this.node.active= false
    }
}


